<?php
require_once "data/dao/BaseDAO.php";

/**
 *
 *   http://www.iso.org/iso/country_codes/iso_3166_code_lists/english_country_names_and_code_elements.htm#u..
 *
 */
class IpToCountryDAO extends BaseDAO {
	
	/**
	 * returns the country name for the given ip
	 * address.
	 *
	 * @param String $ip
	 * @return Country name based on ISO 3166
	 */
	public function getISO3166Country($ip){
		
		return $this->queryForString("SELECT COUNTRY_NAME FROM ipToCountry
			WHERE IP_FROM <= %s and IP_TO >= %s", array($ip, $ip));
	}
	
	/**
	 * returns a two character country code for the given ip
	 * address.
	 *
	 * @param String $ip
	 * @return Two character country code based on 
	 * ISO 3166 alpha 2 code
	 */
	public function getISO3166A2Country($ip){
		
		return $this->queryForString("SELECT COUNTRY_CODE2 FROM ipToCountry
			WHERE IP_FROM <= inet_aton('%s') and IP_TO >= inet_aton('%s')", array($ip, $ip));
	}
	
	/**
	 * returns a three character country code for the given ip
	 * address.
	 *
	 * @param String $ip
	 * @return Three character country code based on ISO 3166
	 */
	public function getISO3166A3Country($ip){
		
		return $this->queryForString("SELECT COUNTRY_CODE3 FROM ipToCountry
			WHERE IP_FROM <= %s and IP_TO >= %s", array($ip, $ip));
	}
}
?>